package com.flyco.modules.stm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.flyco.modules.stm.entity.StmMove;
import com.flyco.modules.stm.entity.StmMoveItem;
import com.flyco.modules.stm.mapper.StmMoveItemMapper;
import com.flyco.modules.stm.mapper.StmMoveMapper;
import com.flyco.modules.stm.service.IStmMoveService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 门店移库单表
 * @Author: flyco
 * @Date:   2021-01-15
 * @Version: V1.0
 */
@DS("store")
@Service
public class StmMoveServiceImpl extends ServiceImpl<StmMoveMapper, StmMove> implements IStmMoveService {

	@Autowired
	private StmMoveMapper stmMoveMapper;
	@Autowired
	private StmMoveItemMapper stmMoveItemMapper;
	
	@Override
	@Transactional
	public void saveMain(StmMove stmMove, List<StmMoveItem> stmMoveItemList) {
		stmMoveMapper.insert(stmMove);
		for(StmMoveItem entity:stmMoveItemList) {
			//外键设置
			entity.setMoveId(stmMove.getId());
			stmMoveItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(StmMove stmMove,List<StmMoveItem> stmMoveItemList) {
		stmMoveMapper.updateById(stmMove);
		
		//1.先删除子表数据
		stmMoveItemMapper.deleteByMainId(stmMove.getId());
		
		//2.子表数据重新插入
		for(StmMoveItem entity:stmMoveItemList) {
			//外键设置
			entity.setMoveId(stmMove.getId());
			stmMoveItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		stmMoveItemMapper.deleteByMainId(id);
		stmMoveMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			stmMoveItemMapper.deleteByMainId(Long.valueOf(id.toString()));
			stmMoveMapper.deleteById(id);
		}
	}
	
}
